#
# Copyright (C) 2015 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=shadowsocks
# PKG_VERSION:=2.2.2
PKG_VERSION:=$(shell LC_ALL=C wget -O - https://github.com/madeye/shadowsocks-libev.git 2>/dev/null|grep -o 'Current version:\s*[0-9.]\+' | grep -o '[0-9.]\+')
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/shadowsocks/shadowsocks-libev.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_RELEASE)
# PKG_SOURCE_VERSION:=4883903e657095b93f88a3a3b9a0dccdffdaa397
PKG_SOURCE_VERSION:=$(shell LC_ALL=C wget -O - https://github.com/madeye/shadowsocks-libev.git 2>/dev/null|grep '>latest commit'|grep -o '\w\{40\}')
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MAINTAINER:=Jian Chang <aa65535@live.com>

PKG_LICENSE:=GPLv2
PKG_LICENSE_FILES:=LICENSE

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_NAME)-$(PKG_VERSION)-$(PKG_RELEASE)

PKG_INSTALL:=1
PKG_FIXUP:=autoreconf
PKG_USE_MIPS16:=0
PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

define Package/shadowsocks/Default
	SECTION:=net
	CATEGORY:=Network
	TITLE:=Lightweight Secured Socks5 Proxy $(2)
	URL:=https://github.com/shadowsocks/shadowsocks-libev
	VARIANT:=$(1)
	DEPENDS:=$(3) +resolveip +ipset +ip +iptables-mod-tproxy
endef

Package/shadowsocks = $(call Package/shadowsocks/Default,openssl,(OpenSSL),+libopenssl)
Package/shadowsocks-polarssl = $(call Package/shadowsocks/Default,polarssl,(PolarSSL),+libpolarssl)

define Package/shadowsocks/description
Shadowsocks is a lightweight secured scoks5 proxy for embedded devices and low end boxes.
endef

Package/shadowsocks-polarssl/description = $(Package/shadowsocks/description)

define Package/shadowsocks/conffiles
/etc/config/shadowsocks
endef

Package/shadowsocks-polarssl/conffiles = $(Package/shadowsocks/conffiles)

define Package/shadowsocks/postinst
#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] || {
	( . /etc/uci-defaults/shadowsocks ) && rm -f /etc/uci-defaults/shadowsocks
}
exit 0
endef

Package/shadowsocks-polarssl/postinst = $(Package/shadowsocks/postinst)

define Package/shadowsocks/prerm
#!/bin/sh
uci -q batch <<-EOF >/dev/null
	delete firewall.shadowsocks
	commit firewall
EOF
/etc/init.d/shadowsocks disable >/dev/null 2>&1
exit 0
endef

Package/shadowsocks-polarssl/prerm=$(Package/shadowsocks/prerm)

ifeq ($(BUILD_VARIANT),polarssl)
	CONFIGURE_ARGS += --with-crypto-library=polarssl
endif

define Package/shadowsocks/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/ss-{local,redir,tunnel,server} $(1)/usr/bin
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/shadowsocks.init $(1)/etc/init.d/shadowsocks
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/shadowsocks.config $(1)/etc/config/shadowsocks
	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
	$(INSTALL_BIN) ./files/shadowsocks.hotplug $(1)/etc/hotplug.d/iface/90-shadowsocks
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) ./files/shadowsocks.firewall $(1)/etc/firewall.shadowsocks
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/shadowsocks.uci $(1)/etc/uci-defaults/shadowsocks
endef

Package/shadowsocks-polarssl/install = $(Package/shadowsocks/install)

$(eval $(call BuildPackage,shadowsocks))
$(eval $(call BuildPackage,shadowsocks-polarssl))